/*{ Begin Form---------------------------------------*/
;
(function(){
	jQuery.fn.ymMasonry = function(options) {
		var defaults = {}
								 
		var options =  $.extend(defaults, options);	
		
		function init(options){
			$('#container').masonry({
				// options
				itemSelector : '.item',
				isAnimated: false,
				bufferPx: 14000,
				extraScrollPx: 14000
			});
			
			flag = true;
			iNextPage = options['startPage'];
			totalPage = options['totalPage'];
			function loadMore()
			{
				if( flag && (iNextPage<=totalPage))
				{
					flag = false;
					$.ajax({
						'url': options['url'],
						'data':{
							page: iNextPage,
							form_submitted: options['form_submitted'],
							keyword: options['keyword'],
							id_category: options['id_category'],
							item_count:  options['item_count'],
							sort	  : options['sort'],
							layout	  : options['layout']
						},
						'success': function(data){
							$data = $(data);
							var iLoad = 0;
							$data.find('img').each(function(){
								$(this).load(function(){
									iLoad++;
									if(iLoad==$data.find('img').length)
									{
										setTimeout(function(){
											$('#container').append($data).masonry('appended', $data, true);
											FB.XFBML.parse(document.getElementById("container"));
										},500);
										iNextPage++;
										flag = true;
									}
								});
							});
						}
					});	
				}
			}
			(f = function(){		
				FB.Canvas.getPageInfo(
					function(info) {
						var top = info.clientHeight + info.scrollTop;				
						var topPaging = $("#master_container").outerHeight() + $("#master_container").offset().top;
						if( (topPaging  + info.offsetTop) <= (info.clientHeight + info.scrollTop))
						{
							loadMore();	
						}
						FB.Canvas._pageInfo = {
							clientWidth:0,
							clientHeight:0,
							scrollLeft:0,
							scrollTop:0,
							offsetLeft:0,
							offsetTop:0
						};
						setTimeout(f,500);
					}
					);
			})();
		}
					
		return this.each(function() {
			if ( options ) {
				init.call(this,options);
			}
		});
	}
})(jQuery);